
 /**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */
package com.sys.midware.redis.core.operation.impl;

import java.util.Set;

import org.apache.log4j.Logger;

import com.sys.midware.redis.core.RedisFactory;
import com.sys.midware.redis.core.operation.AbstractOperations;
import com.sys.midware.redis.core.operation.SetOperations;
import com.sys.midware.redis.exceptions.BRedisException;
import com.sys.midware.redis.util.SerializationUtils;

import redis.clients.jedis.JedisCluster;

/**
 * <b>Description：</b> set 类型操作类 <br/>
 * <b>ClassName：</b> SetOpertions <br/>
 * <b>@author：</b> jackyshang <br/>
 * <b>@date：</b> 2016年8月22日 下午4:13:07 <br/>
 * <b>@version: </b>  <br/>
 */
public class ClusterSetOpertionsImpl extends AbstractOperations implements SetOperations{
    
    private static Logger logger = Logger.getLogger(StringsOperationsImpl.class);
    private ClusterSetOpertionsImpl() {
    };

    private static ClusterSetOpertionsImpl instance;
    
    /**
     * 获取单例工厂
     * @return
     * @throws Exception
     */
    public static ClusterSetOpertionsImpl getInstance() {
        if (instance != null)
            return instance;

        synchronized (ClusterSetOpertionsImpl.class) {
            if (instance == null) {
                instance = new ClusterSetOpertionsImpl();
            }
            return instance;
        }
    }

    
    @Override
    public <K, V> Long putSet(K key, Set<V> values) {
        JedisCluster jedis = getClient4Cluster();
        try {
        Long l=  jedis.sadd(rawKey(key),rawValues(values) );
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Set<V> getFromSet(K key) {
        JedisCluster jedis = getClient4Cluster();
        try {
          Set<byte[]> re=  jedis.smembers(rawKey(key));
            return SerializationUtils.deserialize(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K> Long remove(K key, Object... values) {
        JedisCluster jedis = getClient4Cluster();
        try {
        Long re=  jedis.srem(rawKey(key), rawValues(values));
            return re;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K> Long size(K key) {
        JedisCluster jedis = getClient4Cluster();
        try {
          Long re=  jedis.scard(rawKey(key));
            return re;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K> Long add(K key, Object... values) {
        JedisCluster jedis = getClient4Cluster();
        try {
        Long l=  jedis.sadd(rawKey(key),rawValues(values) );
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> V pop(K key) {
        JedisCluster jedis = getClient4Cluster();
        try {
            byte[] re=  jedis.spop(rawKey(key));
            return deserializeValue(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K> Boolean isMember(K key, Object value) {
        JedisCluster jedis = getClient4Cluster();
        try {
          Boolean re=  jedis.sismember(rawKey(key), rawValue(value));
            return re;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Set<V> difference(Object... keys) {
        JedisCluster jedis = getClient4Cluster();
        try {
          Set<byte[]> re=  jedis.sdiff(rawKeys(keys));
            return SerializationUtils.deserialize(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }
    @Override
    public <K, V> Set<V> union(Object... keys) {
        JedisCluster jedis = getClient4Cluster();
        try {
          Set<byte[]> re=  jedis.sunion(rawKeys(keys));
            return SerializationUtils.deserialize(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }


}
